//
//  YCUserAccount.swift
//  swift微博
//
//  Created by 姚伟聪 on 15/12/19.
//  Copyright © 2015年 姚伟聪. All rights reserved.
//

//因为在多个地方可能都要用到，所以建成全局的模型
import UIKit

class YCUserAccount: NSObject,NSCoding {

    
    class var isUseLogin:Bool {
    
        return YCUserAccount.loadUserAccount() != nil
    
    }
    
    var access_token: String?
    //官网说是string，其实是int
    var expires_in: NSTimeInterval = 0{
    
        didSet{
           
            expires_Date = NSDate(timeIntervalSinceNow: expires_in)
          
        }
    
    }
    var uid: String?
    
    //过期时间
    var expires_Date:NSDate?
    
    /// 用户昵称
    var screen_name: String?
    
    /// 用户头像地址（大图），180×180像素
    var avatar_large: String?
    
    init(dict:[String:AnyObject]) {
        
        super.init()
        setValuesForKeysWithDictionary(dict)

    }
    //还要防止dict中又本来没有的属性，所以才重写这个方法
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        
    }
    
    //MARK: - 归档
    func encodeWithCoder(aCoder: NSCoder) {
        
        aCoder.encodeObject(access_token, forKey: "access_token")
//        aCoder.encodeDouble(expires_in, forKey: "expires_in")
        aCoder.encodeObject(uid, forKey: "uid")
        aCoder.encodeObject(expires_Date, forKey: "expires_Date")
         aCoder.encodeObject(screen_name, forKey: "screen_name")
         aCoder.encodeObject(avatar_large, forKey: "avatar_large")
        
        
    }
    //MARK: - 反归档
    required init?(coder aDecoder: NSCoder) {
        
        access_token =  aDecoder.decodeObjectForKey("access_token") as? String
        uid = aDecoder.decodeObjectForKey("uid") as? String
        expires_Date = aDecoder.decodeObjectForKey("expires_Date") as? NSDate
        screen_name  = aDecoder.decodeObjectForKey("screen_name") as? String
        avatar_large  = aDecoder.decodeObjectForKey("avatar_large") as? String
        
    }
    
    //保存的沙盒路径
    
   static let path = (NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("user.plist")
    
    //保存用户信息
    func saveUserAccount(){
        
        //归档
        NSKeyedArchiver.archiveRootObject(self, toFile: YCUserAccount.path)
        
    
    }
    
    //加载用户信息
    func loadUsetInfo(finish: (error: NSError?)-> Void)  {
    
        YCNetWorkTool.shardNetWorkTool.loadUserInfo { (result, error) -> () in
            
            self.screen_name = result!["screen_name"] as? String
            self.avatar_large = result!["avatar_large"] as? String
            print("access_token\(self.access_token)")
            self.saveUserAccount()
            
            finish(error: nil)
            
        }
        
    
    
    }
    
    
    class func loadUserAccount() -> YCUserAccount? {
    
        if let userAccount = NSKeyedUnarchiver.unarchiveObjectWithFile(path) as? YCUserAccount{
        
            
            //比较，是升序降序
            if userAccount.expires_Date?.compare(NSDate()) == NSComparisonResult.OrderedDescending{
                print("账号可用")
                
                return userAccount
            
            }
            else
            {
                
                return nil
            }
            
        }
        else{
        
           
        }
         return nil
        
        
    }
    
}
